package com.mhxy.service.impl;

import com.mhxy.constant.OpencvConstants;
import com.mhxy.constant.PicConstants;
import com.mhxy.enums.Coord;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.FuzzyRectPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 出售临时符
 */
@Slf4j
public class SellTempSymbolTaskImpl extends AbstractTask {
    public SellTempSymbolTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc3();
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_商城, 3).isFinded()) {
            return true;
        }
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        ThreadUtil.sleep1();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_商城, 5)) {
            log.error("【{}】没有找到 商城", getTaskName());
            return false;
        }

        ThreadUtil.sleep1();
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.商城_已选择摊位,3,0.95).isFinded()) {
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, PicConstants.商城_未选择摊位)) {
                log.error("【{}】摆摊图标未找到！退出任务");
                return false;
            }
        }

        ThreadUtil.sleep1();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.商城_我要出售, 5)) {
            log.error("【{}】没有找到 我要出售", getTaskName());
            return false;
        }

        ThreadUtil.sleep1();
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.商城_摊位已满, 3, 0.99).isFinded()) {
            clickEsc1();
            log.info("【{}】摊位已满", getTaskName());
            TaskProgressHandler.updateProgress(getTaskName(), "已满");
            return false;
        }

        return true;
    }

    @Override
    public Boolean after() throws Exception {
        boolean isClick7Days = false;
        for (int i = 0; i < 8; i++) {
            if (!FuzzyRectPicUtil.findAndClickRandom(hwnd_0, PicConstants.商城_临时符, OpencvConstants.STORE_SELL_RECT, 3)) {
                log.info("【{}】包裹里面没有临时符了", getTaskName());
                return true;
            }
            FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.商城_本服上架, 3);
            ThreadUtil.sleep1();
            // 7天不再提示
            if (!isClick7Days && FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.商城_7天不在提示, 1)) {
                isClick7Days = true;
                FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.商城_7天不在提示确定, 3);
            }
        }
        clickEsc3();
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "出售临时符";
    }
}
